Page({
  data: {
    checked: [true, false, false], // 用一个数组来保存复选框的状态
  },

  onChange(event) {
    const index = event.currentTarget.dataset.index;
    const checked = this.data.checked;

    // 检查是否已经有一个复选框被选中
    const currentCheckedIndex = checked.indexOf(true);

    // 如果已经有一个默认地址并且点击的不是同一个
    if (currentCheckedIndex !== -1 && currentCheckedIndex !== index) {
      wx.showToast({
        title: '请选择一个默认地址',
        icon: 'none',
      });

      // 取消当前的选择
      const newChecked = checked.slice();
      newChecked[index] = false;
      this.setData({
        checked: newChecked,
      });
    } else {
      // 更新复选框状态
      const newChecked = checked.map((item, idx) => idx === index);
      newChecked[index] = true;
      this.setData({
        checked: newChecked,
      });
    }
  },

  onDelete: function(event) {
    const id = event.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '您确定要删除这个地址吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            addresses: this.data.addresses.filter(address => address.id !== id)
          });
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        } else if (res.cancel) {
          wx.showToast({
            title: '取消删除',
            icon: 'none'
          });
        }
      }
    });
  },
  onupdate:function(){
    wx.navigateTo({
      url: '/pages/znzs-xq-address-xq/znzs-xq-address-xq',
    })
  }
});